package com.getpebble.android.framework.app;

import android.content.ContentValues;
import android.text.TextUtils;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ObjectUtil;
import com.getpebble.android.common.framework.install.app.AppBundle;
import com.getpebble.android.common.model.AppVersion;
import com.getpebble.android.common.util.CollectionUtil;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppBankInfoInstallMessage.BankInfoEntry;
import com.google.common.primitives.UnsignedInteger;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class InstalledApp {
    private static final String TAG = InstalledApp.class.getSimpleName();
    private String mAppName;
    private int mBankNumber;
    private List<String> mCapabilities;
    private String mCompanyName;
    private String mDeviceAddress;
    private UnsignedInteger mFlags;
    private Integer mId;
    private int mInstallId;
    private UUID mUuid;
    private AppVersion mVersion;

    public static class AppCompanyVersionTuple {
        private String mAppName;
        private String mCompanyName;
        private AppVersion mVersion;

        public AppCompanyVersionTuple(String appName, String companyName, AppVersion version) {
            if (appName == null) {
                throw new IllegalArgumentException("'appName' cannot be null!");
            } else if (companyName == null) {
                throw new IllegalArgumentException("'companyName' cannot be null!");
            } else {
                this.mAppName = appName;
                this.mCompanyName = companyName;
                this.mVersion = version;
            }
        }

        public int hashCode() {
            int hash = ((this.mAppName.hashCode() + 1067) * 97) + this.mCompanyName.hashCode();
            if (this.mVersion != null) {
                return (hash * 97) + this.mVersion.hashCode();
            }
            return hash;
        }

        public boolean equals(Object o) {
            boolean z = true;
            if (o == null) {
                return false;
            }
            if (o == this) {
                return true;
            }
            if (!(o instanceof AppCompanyVersionTuple)) {
                return false;
            }
            AppCompanyVersionTuple other = (AppCompanyVersionTuple) o;
            if (!(this.mAppName.equals(other.mAppName) && this.mCompanyName.equals(other.mCompanyName) && ObjectUtil.nullCheckEquals(this.mVersion, other.mVersion))) {
                z = false;
            }
            return z;
        }
    }

    public Integer getId() {
        return this.mId;
    }

    public String getDeviceAddress() {
        return this.mDeviceAddress;
    }

    public UUID getUuid() {
        return this.mUuid;
    }

    public String getUuidString() {
        if (this.mUuid != null) {
            return this.mUuid.toString();
        }
        return null;
    }

    public String getAppName() {
        return this.mAppName;
    }

    public String getCompanyName() {
        return this.mCompanyName;
    }

    public int getInstallId() {
        return this.mInstallId;
    }

    public int getBankNumber() {
        return this.mBankNumber;
    }

    public AppVersion getVersion() {
        return this.mVersion;
    }

    public UnsignedInteger getFlags() {
        return this.mFlags;
    }

    public AppCompanyVersionTuple getTuple() {
        return new AppCompanyVersionTuple(getAppName(), getCompanyName(), getVersion());
    }

    public List<String> getCapabilities() {
        return this.mCapabilities;
    }

    private void setId(Integer id) {
        this.mId = id;
    }

    private void setDeviceAddress(String deviceAddress) {
        this.mDeviceAddress = deviceAddress;
    }

    public void setUuid(UUID uuid) {
        this.mUuid = uuid;
    }

    private void setAppName(String appName) {
        this.mAppName = appName;
    }

    private void setCompanyName(String companyName) {
        this.mCompanyName = companyName;
    }

    private void setInstallId(int installId) {
        this.mInstallId = installId;
    }

    private void setBankNumber(int bankNumber) {
        this.mBankNumber = bankNumber;
    }

    private void setVersion(AppVersion version) {
        this.mVersion = version;
    }

    private void setFlags(UnsignedInteger flags) {
        this.mFlags = flags;
    }

    public void setCapabilities(String[] capabilities) {
        if (capabilities != null) {
            this.mCapabilities = CollectionUtil.convertToLowercaseList(Arrays.asList(capabilities));
        }
    }

    public boolean needsUpdateFrom(BankInfoEntry entry) {
        boolean valuesEqual;
        if (getInstallId() == entry.getInstallId() && getBankNumber() == entry.getBankNumber() && ObjectUtil.nullCheckEquals(getAppName(), entry.getAppName()) && ObjectUtil.nullCheckEquals(getCompanyName(), entry.getCompanyName()) && ObjectUtil.nullCheckEquals(getFlags(), entry.getFlags()) && ObjectUtil.nullCheckEquals(getVersion(), entry.getVersionNumber())) {
            valuesEqual = true;
        } else {
            valuesEqual = false;
        }
        return !valuesEqual;
    }

    public void updateFrom(BankInfoEntry entry) {
        setInstallId(entry.getInstallId());
        setBankNumber(entry.getBankNumber());
        setAppName(entry.getAppName());
        setCompanyName(entry.getCompanyName());
        setFlags(entry.getFlags());
        setVersion(entry.getVersionNumber());
    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues(9);
        if (getId() != null) {
            contentValues.put("_id", getId());
        }
        contentValues.put("device_address", getDeviceAddress());
        contentValues.put("uuid", getUuidString());
        contentValues.put("app_name", getAppName());
        contentValues.put("company_name", getCompanyName());
        contentValues.put("install_id", Integer.valueOf(getInstallId()));
        contentValues.put("bank_number", Integer.valueOf(getBankNumber()));
        if (getFlags() != null) {
            contentValues.put("flags", Long.valueOf(getFlags().longValue()));
        }
        if (getVersion() != null) {
            Trace.debug(TAG, "toContentValues() storing version: getVersion().getVersionTag()");
            contentValues.put("version", getVersion().getVersionTag());
        }
        if (getCapabilities() != null) {
            contentValues.put("capabilities", TextUtils.join(",", getCapabilities()));
        }
        return contentValues;
    }

    public static InstalledApp fromContentValues(ContentValues contentValues) {
        InstalledApp installedApp = new InstalledApp();
        installedApp.setId(contentValues.getAsInteger("_id"));
        installedApp.setDeviceAddress(contentValues.getAsString("device_address"));
        String uuidStr = contentValues.getAsString("uuid");
        if (uuidStr != null) {
            installedApp.setUuid(UUID.fromString(uuidStr));
        }
        installedApp.setAppName(contentValues.getAsString("app_name"));
        installedApp.setCompanyName(contentValues.getAsString("company_name"));
        installedApp.setInstallId(contentValues.getAsInteger("install_id").intValue());
        installedApp.setBankNumber(contentValues.getAsInteger("bank_number").intValue());
        installedApp.setVersion(new AppVersion(contentValues.getAsString("version")));
        Long flags = contentValues.getAsLong("flags");
        if (flags != null) {
            installedApp.setFlags(UnsignedInteger.valueOf(flags.longValue()));
        }
        String capabilitiesText = contentValues.getAsString("capabilities");
        if (!TextUtils.isEmpty(capabilitiesText)) {
            String[] capabilities = capabilitiesText.split(",");
            if (capabilities != null) {
                installedApp.setCapabilities(capabilities);
            }
        }
        return installedApp;
    }

    public static InstalledApp fromBankInfoEntry(BankInfoEntry entry, String deviceAddress) {
        InstalledApp installedApp = new InstalledApp();
        installedApp.setDeviceAddress(deviceAddress);
        installedApp.updateFrom(entry);
        return installedApp;
    }

    public static InstalledApp fromAppBundle(AppBundle appBundle, int bankNumber, String deviceAddress) {
        InstalledApp installedApp = new InstalledApp();
        installedApp.setDeviceAddress(deviceAddress);
        installedApp.setUuid(appBundle.getAppInfo().getUuid());
        installedApp.setAppName(appBundle.getAppInfo().getShortName());
        installedApp.setCompanyName(appBundle.getAppInfo().getCompanyName());
        installedApp.setBankNumber(bankNumber);
        installedApp.setVersion(new AppVersion(appBundle.getAppInfo().getVersionLabel()));
        installedApp.setCapabilities(appBundle.getAppInfo().getCapabilities());
        return installedApp;
    }
}
